package MyServlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import service.ServiceException;
import ServletService.Service;
import model.Candidate;
import model.Department;
import model.Interview;
import model.Position;
import model.Role;
import model.User;

/**
 * Servlet implementation class login
 */

public class login extends HttpServlet {
	private static final long serialVersionUID = 1L;
	String user;
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public login() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String[] roles = new String[5];
		roles[0] = "Director General";
		roles[1] = "Recruiter";
		roles[2] = "Administrator";
		roles[3] = "Director de Departament";
		roles[4] = "Director HR";
		request.setAttribute("roles", roles);
		if(request.getAttribute("role")==null){
			request.setAttribute("role", roles[0]);
		}
		try{
			HttpSession session = request.getSession(true);
			user = (String) session.getAttribute("user");
		}catch(Exception e){
			
		}
		System.out.println(user);
		//String currentUser = (String) request.getAttribute("user");
		//List<Role> roles = Service.getUserService().getUserByUsername(user).getRoles();
	
//		int cnt=0;
//		String[] rolesList = new String[roles.size()];
//		for (Role r : roles){
//			rolesList[cnt] = r.getTitle();
//			cnt++;		
//		}
//		request.setAttribute("roles", rolesList);
//		request.setAttribute("role", rolesList[0]);
		
		List<User> userList = new ArrayList<>();
		userList = Service.getUserService().getAllUsers();
		request.setAttribute("userList", userList);

		List<User> userActiveList = new ArrayList<>();
		userActiveList = Service.getUserService().getAllActiveUsers();
		request.setAttribute("userActiveList", userActiveList);

		List<Position> approvedPositionList = new ArrayList<>();

		approvedPositionList = Service.getPositionService()
				.getApprovedPositions();

		request.setAttribute("approvedPositionList", approvedPositionList);

		List<Position> unApprovedPositionList = new ArrayList<>();

		unApprovedPositionList = Service.getPositionService()
				.getUnApprovedPositions();

		request.setAttribute("unApprovedPositionList", unApprovedPositionList);
		
		List<Interview> interviewsList = new ArrayList<>();

		interviewsList = Service.getInterviewService().getAll();

		request.setAttribute("interviewsList", interviewsList);

		if (request.getAttribute("candidateList") == null) {
			List<Candidate> candidateList = new ArrayList<>();

			candidateList = Service.getCandidateService().getAll();

			request.setAttribute("candidateList", candidateList);
		}
		request.setAttribute("user", user);
		RequestDispatcher rd = request.getRequestDispatcher("/loggedIn.jsp");
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("sunt in post");
		/*
		 * response.encodeUrl("authors.jsp");
		 * request.getRequestDispatcher("index.jsp").forward(request, response);
		 */
		/*user = request.getParameter("user");
		String password = request.getParameter("pass");

		request.setAttribute("user", user);
		response.sendRedirect(request.getContextPath() + "/login");*/
		RequestDispatcher rd = request.getRequestDispatcher("/loggedIn.jsp");
		rd.forward(request, response);
	}

}
